package com.example.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wemedia.dao.WmMaterialDao;
import com.example.wemedia.dao.WmNewsDao;
import com.example.wemedia.dao.WmNewsMaterialDao;
import com.example.wemedia.dao.WmUserDao;
import com.example.wemedia.service.WmNewsService;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class WmNewsServiceImpl implements WmNewsService {
    
    @Autowired
    private WmNewsDao wmNewsDao;
    
    @Autowired
    private WmUserDao wmUserDao;
    
    @Autowired
    private WmMaterialDao wmMaterialDao;
    
    @Autowired
    private WmNewsMaterialDao wmNewsMaterialDao;
   
    
    /**
     * 查询
     *
     * @param wmNewsPageReqDto
     * @return
     */
    @Override
    public ResponseResult getAllNew(WmNewsPageReqDto wmNewsPageReqDto) {
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        if (wmNewsPageReqDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //分页参数校验
        wmNewsPageReqDto.checkParam();
        if (wmNewsPageReqDto.getKeyword() != null) {
            queryWrapper.like("title", wmNewsPageReqDto.getKeyword());
        }
        if (wmNewsPageReqDto.getChannelId() != null) {
            queryWrapper.like("channel_id", wmNewsPageReqDto.getChannelId());
        }
        if (wmNewsPageReqDto.getStatus() != null) {
            queryWrapper.like("status", wmNewsPageReqDto.getStatus());
        }
        if (wmNewsPageReqDto.getBeginPubDate() != null && wmNewsPageReqDto.getEndPubDate() != null) {
            queryWrapper.between("publish_time", wmNewsPageReqDto.getBeginPubDate(), wmNewsPageReqDto.getEndPubDate());
        }
        Page<WmNews> wmNews = new Page<WmNews>(wmNewsPageReqDto.getPage(), wmNewsPageReqDto.getSize());
        IPage<WmNews> wmNewsIPage = wmNewsDao.selectPage(wmNews, queryWrapper);
        List<WmNews> records = wmNewsIPage.getRecords();
        return ResponseResult.okResult(records);
    }
    
    
    /**
     * 新增或者修改自媒体信息
     *
     * @param wmNewsDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult submit(WmNewsDto wmNewsDto) {
        if (wmNewsDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (wmNewsDto.getId() == null) {//添加
            WmNews wmNews = new WmNews();
            BeanUtils.copyProperties(wmNewsDto, wmNews);
            List<String> images = wmNewsDto.getImages();
            log.info("前台图片列表images:{}", images);
            String newsImgUrl = "";
            //保存截取后的url
            List<String> imgList = new ArrayList<>();
            for (String imgUrl : images) {
                String substringUrl = StringUtils.substringPath(imgUrl);
                log.info("截取后的图片url:{}", substringUrl);
                imgList.add(substringUrl);
                newsImgUrl += substringUrl + ",";
            }
            String img = "";
            if (!CollectionUtils.isEmpty(images)) {
                img = newsImgUrl.substring(0, newsImgUrl.length() - 1);
            }
            
            log.info("保存的imgUrl:{}", img);
            wmNews.setImages(img);
            wmNews.setEnable(1);
            wmNews.setStatus(1);
            wmNews.setCreatedTime(new Date());
            wmNews.setSubmitedTime(wmNews.getCreatedTime());
            wmNewsDao.insert(wmNews);
            Integer wmNewsId = wmNews.getId();
            log.info("wmNewsId:{}", wmNewsId);
            //查询
            LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (null != imgList) {
                lambdaQueryWrapper.in(WmMaterial::getUrl, imgList);
            }
            List<WmMaterial> wmMaterials = wmMaterialDao.selectList(lambdaQueryWrapper);
            QueryWrapper<WmMaterial> queryWrapper = new QueryWrapper<>();
            
            //批量保存
            for (WmMaterial wmMaterial : wmMaterials) {
                WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
                wmNewsMaterial.setId(100);
                wmNewsMaterial.setNewsId(wmNewsId);
                wmNewsMaterial.setMaterialId(wmMaterial.getId());
                wmNewsMaterialDao.insert(wmNewsMaterial);
            }
            System.out.println(wmMaterials.toString());
        }
        return ResponseResult.okResult("");
    }
    
    /**
     * 添加wm用户
     *
     * @param wmUserDto wm用户dto
     * @return {@code Integer}
     */
    @Override
    public Integer addWmUser(WmUserDto wmUserDto) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(wmUserDto, wmUser);
        wmUser.setCreatedTime(new Date());
        
        return wmUserDao.insert(wmUser);
    }
    
    /**
     * 得到wm新闻通过id
     *
     * @param id id
     * @return {@code WmNews}
     */
    @Override
    public WmNews getWmNewsById(Integer id) {
        return wmNewsDao.selectById(id);
        
    }
    
    /**
     * 更新wm新闻通过id
     *
     * @param wmNews wm新闻
     * @return {@code Integer}
     */
    @Override
    public Integer updateWmNewsById(WmNews wmNews) {
        return wmNewsDao.updateById(wmNews);
        
    }
}
